package com.mswork.mes.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.mswork.common.core.domain.model.LoginUser;
import com.mswork.common.utils.SecurityUtils;
import com.mswork.mes.domain.vo.ProjectVo;
import com.mswork.mes.utils.AuthorizeUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.mswork.common.annotation.Log;
import com.mswork.common.core.controller.BaseController;
import com.mswork.common.core.domain.AjaxResult;
import com.mswork.common.enums.BusinessType;
import com.mswork.mes.domain.Project;
import com.mswork.mes.service.IProjectService;
import com.mswork.common.utils.poi.ExcelUtil;
import com.mswork.common.core.page.TableDataInfo;

/**
 * 项目管理Controller
 * 
 * @author manshu
 * @date 2025-03-03
 */
@RestController
@RequestMapping("/mes/project")
public class ProjectController extends BaseController
{

    @Autowired
    private IProjectService projectService;

    /**
     * 查询项目管理列表
     */
    //@PreAuthorize("@ss.hasPermi('mes:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(Project project)
    {
        startPage();
        //List<ProjectVo> list = projectService.selectProjectListV2(project);
        List<Project> list = projectService.selectProjectList(project);
        return getDataTable(list);
    }

    //@PreAuthorize("@ss.hasPermi('mes:project:detail')")
    @GetMapping("/detail")
    public TableDataInfo listV2(Project project)
    {
        startPage();
        //AuthorizeUtils.hasPermi();
        List<ProjectVo> list = projectService.selectProjectListV2(project);
        return getDataTable(list);
    }

    /**
     * 导出项目管理列表
     */
    //@PreAuthorize("@ss.hasPermi('mes:project:export')")
    @Log(title = "项目管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Project project)
    {
        List<Project> list = projectService.selectProjectList(project);
        ExcelUtil<Project> util = new ExcelUtil<Project>(Project.class);
        util.exportExcel(response, list, "项目管理数据");
    }

    /**
     * 获取项目管理详细信息
     */
   // @PreAuthorize("@ss.hasPermi('mes:project:query')")
    @GetMapping(value = "/{projectId}")
    public AjaxResult getInfo(@PathVariable("projectId") Integer projectId)
    {
        //return success(projectService.selectProjectByProjectId(projectId));
        return success(projectService.selectProjectDetails(projectId));
    }


    /**
     * 新增项目管理
     */
    //@PreAuthorize("@ss.hasPermi('mes:project:add')")
    @Log(title = "项目管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Project project)
    {
        if(!AuthorizeUtils.hasPermi()){
            return toAjax(projectService.insertProject(project));
        }else {
            return AjaxResult.warn("体验用户,无法使用该功能");
        }

    }

    /**
     * 修改项目管理
     */
    //@PreAuthorize("@ss.hasPermi('mes:project:edit')")
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Project project)
    {
        if(!AuthorizeUtils.hasPermi()){
            return toAjax(projectService.updateProject(project));
        }else {
            return AjaxResult.warn("体验用户,无法使用该功能");
        }
    }

    /**
     * 删除项目管理
     */
    //@PreAuthorize("@ss.hasPermi('mes:project:remove')")
    @Log(title = "项目管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{projectIds}")
    public AjaxResult remove(@PathVariable Integer[] projectIds)
    {
        if(!AuthorizeUtils.hasPermi()){
            return toAjax(projectService.deleteProjectByProjectIds(projectIds));
        }else {
            return AjaxResult.warn("体验用户,无法使用该功能");
        }
    }


    /**
     * 查询所有项目和明细
     */
    //@PreAuthorize("@ss.hasPermi('mes:project:all')")
    @GetMapping("/all")
    public TableDataInfo all(Project project)
    {
        List<ProjectVo> list = projectService.selectProjectAll(project);
        return getDataTable(list);
    }
}
